package com.yisin.www.chat;

import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.yisin.pbox.frame.common.RootContext;
import com.yisin.pbox.frame.util.StringUtil;

public class ChatRobot {

	public static Map<String, Map<String, String>> userMap = new HashMap<String, Map<String, String>>();
	public static Map<String, String> ipUser = new HashMap<String, String>();
	public static Map<String, String> names = new HashMap<String, String>();

	public static String[] ipArray = { "61.179.123.山东省日照市", "61.179.113.山东省青岛市", "61.176.192.辽宁省大连市", "61.168.244.河南省焦作市", "61.167.199.黑龙江省大庆市", "61.159.11.河北省石家庄市",
			"61.157.91.四川省成都市", "61.149.171.北京市朝阳区", "61.149.173.北京市丰台区", "61.149.11.北京市大兴区", "61.149.31.北京市海淀区", "61.149.51.北京市通州区", "210.72.88.云南省昆明市", "61.183.248.湖北省武汉市",
			"119.39.23.湖南省长沙市", "124.228.41.湖南省衡阳市", "202.96.134.广东省深圳市", "210.72.1.广东省广州市", "61.149.71.北京市西城区", "61.149.81.北京市怀柔区" };

	public static String[] nameArray = { "樱花识盈", "余念不残留", "神经质先生", "Mars坏妞", "真的不快乐", "本人、狠低调", "终究还是放不开", "款爷", "笑等茶凉", "背着书包闯天下", "念奴娇", "Smile、想你", "、醜到想整容", "手牵手一起走", "时间而已",
			"时间在渗透", "永恒之塔", "脾气暴躁者", "一朵花开的时间", "大步向前走", "惜你如命", "想发光得交电费", "雪落纷纷", "无人过问", "猜心游戏", "泪城", "泪染倾城", "If", "情歌爸爸", "我的爱等三年", "别让依靠成为习惯", "#禁爱、", "梧桐有语", "阳光少年",
			"钱是我祖宗", "好人好事理事会i", "时间煮雨也煮心", "沫去、", "绝版仅我一枚", "终走", "小别致、", "限量版帅哥", "走过丶和尚路", "唇、", "Trouble Maker", "地势坤唯吾独尊 ", "樱花葬礼", "爱情不算什么", "就这样一直爱你", "碎发少年", "长发菇凉。",
			"丶萝卜你个大白菜", "、白菜你个小番茄", "暖女", "暖男", "可不可以不爱了i", "烟花固然美而绽放", "潇洒哥 温柔姐", "疲惫无力的心", "是我不够温暖", "抑郁症后期", "敌不过句逝去", "声声叹", "迟来的爱", "爱情丶后八轮", "你动我必定败#", "想带在角落", "染指旳沵唇",
			"在傻子面前卖个萌", "吃饭碎觉挤痘痘", "深鱼入画一生天涯", "爱你只有一次", "Cc果冻", "青春美丽疙瘩痘", "逼着放弃最痛苦", "徒留、孤单", "淡淡香水", "哦吧啥哪嘿i", "半粒糖甜到伤", "笑看红尘埃", "若终要失去、不如放开", "惯例", "失退", "天仓优雨",
			"Love萌你心de女人", "请你爱我一回" };

	private static String[] charArray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
			"R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
			"y", "z" };

	public static List<String> msgList = new ArrayList<String>();

	static {
		msgList.add("人咋这么少呢？");
		msgList.add("大家出来聊天啊");
		msgList.add("你们都是干嘛的？");
		msgList.add("你是做什么工作的");
		msgList.add("hi 大家好！");
		msgList.add("挺热闹啊");
		msgList.add("这也挺好玩的，呵呵");
		msgList.add("上班中啊，");
		msgList.add("隐聊，是使用JsSocket开发，详细请访问官方论坛。<br><br><a href=\"http://yisin.88448.com/viewthread.php?tid=39949\" target=\"_blank\">隐心论坛</a>");
		msgList.add("说句话吧，大家们");
		msgList.add("我会回答你一切问题，你尽管问吧");
		msgList.add("只有你想不到，没有我做不到");
		msgList.add("如果你觉得赞，请说句话吧");
		msgList.add("这个地址要收藏哦，记得分享给好友");
		msgList.add("....");
		String path = RootContext.getRootName() + "/pages/chats/images";
		msgList.add("<img src=\"" + path + "/face1/1.gif\">");
		msgList.add("<img src=\"" + path + "/face1/2.gif\">");
		msgList.add("<img src=\"" + path + "/face1/13.gif\">");
		msgList.add("<img src=\"" + path + "/face1/24.gif\">");
		msgList.add("<img src=\"" + path + "/face1/35.gif\">");
		msgList.add("<img src=\"" + path + "/face1/46.gif\">");
		msgList.add("<img src=\"" + path + "/face1/27.gif\">");
		msgList.add("<img src=\"" + path + "/face1/38.gif\">");
		msgList.add("<img src=\"" + path + "/face1/59.gif\">");
		msgList.add("<img src=\"" + path + "/face2/qq5.gif\">");
		msgList.add("<img src=\"" + path + "/face2/qq6.gif\">");
		msgList.add("<img src=\"" + path + "/face2/qq9.gif\">");
		msgList.add("<img src=\"" + path + "/face2/qq15.gif\">");
		msgList.add("<img src=\"" + path + "/face3/2.gif\">");
		msgList.add("<img src=\"" + path + "/face3/6.gif\">");
		msgList.add("<img src=\"" + path + "/face3/15.gif\">");
		msgList.add("<img src=\"" + path + "/face3/24.gif\">");
		msgList.add("<img src=\"" + path + "/face4/14.gif\">");
		msgList.add("<img src=\"" + path + "/face4/4.gif\">");
		msgList.add("<img src=\"" + path + "/face4/24.gif\">");
	}

	public static String[] create() {
		String[] result = new String[9];
		String ips = ipArray[(int) (Math.random() * ipArray.length)];
		int lastIndex = ips.lastIndexOf(".");
		String ip = ips.substring(0, lastIndex) + "." + ((int) (Math.random() * 254));
		String city = ips.substring(lastIndex + 1);
		result[0] = "-" + ip + ":" + ((int) (Math.random() * 9000) + 1);
		result[1] = city;
		// qq
		result[2] = getQq();
		// userId
		result[3] = "R" + getUserId();
		// mobile
		result[4] = getMobile();
		// name
		result[5] = getName();
		// email
		result[6] = getQq() + "@qq.com";
		// header
		int index = (int) (Math.random() * 30 + 1);
		String h = index < 10 ? "0" + index : "" + index;
		result[7] = "pages/chats/images/headImage/00" + h + ".gif";
		String[] sex = { "男", "女" };
		String mi = sex[(int) (Math.random() * 2)];
		result[8] = mi;
		return result;
	}

	public static String getQq() {
		String qq = "" + (int) (Math.random() * 9);
		int len = (int) (Math.random() * 2);
		for (int i = 0; i < (7 + len); i++) {
			qq += (int) (Math.random() * 9);
		}
		return qq;
	}
	
	public static String getUserId() {
		String qq = "";
		int ix = 0;
		int len = (int) (Math.random() * 2);
		for (int i = 0; i < (9 + len); i++) {
			ix = (int) (Math.random() * charArray.length);
			qq += charArray[ix];
		}
		return qq;
	}

	public static String getMobile() {
		String[] m = { "13", "15", "18" };
		int mi = (int) (Math.random() * m.length);
		String mobile = m[mi] + "";
		for (int i = 0; i < 9; i++) {
			mobile += (int) (Math.random() * 9);
		}
		return mobile;
	}

	public static String getName() {
		String name = "";
		name = nameArray[(int) (Math.random() * nameArray.length)];
		int i = 0;
		while (i < 50 && names.containsValue(name)) {
			name = nameArray[(int) (Math.random() * nameArray.length)];
			i++;
		}
		return name;
	}

	public Map<String, String> createRobot(int count) {
		Map<String, String> userInfo = new HashMap<String, String>();
		for (int i = 0; i < count; i++) {
			userInfo = new HashMap<String, String>();
			String[] data = create();
			userInfo.put("ip", data[0]);
			userInfo.put("city", data[1]);
			userInfo.put("qq", data[2]);
			userInfo.put("userId", data[3]);
			userInfo.put("mobile", data[4]);
			userInfo.put("name", data[5]);
			userInfo.put("email", data[6]);
			userInfo.put("header", data[7]);
			userInfo.put("sex", data[8]);

			userMap.put(userInfo.get("ip"), userInfo);
			ipUser.put(userInfo.get("userId"), userInfo.get("ip"));
			names.put(userInfo.get("userId"), userInfo.get("name"));
			ChatServlet.userMap.put(userInfo.get("ip"), userInfo);
			ChatServlet.ipUser.put(userInfo.get("userId"), userInfo.get("ip"));
		}
		return userInfo;
	}

	public void startThread() {
		Thread t = new Thread(new RobotThread());
		t.start();
	}

	public void checkAndRe(Socket tosoc, String fid, String tid, JSONObject msgObj, int timeout) {
		String msg = StringUtil.filterHtml(StringUtil.objectToString(msgObj.get("content"), ""));
		if(msg.trim().length() > 0){
			Thread t = new Thread(new RobotReThread(tosoc, fid, tid, msg, timeout));
			t.start();
		}
	}

	public static void addMsg(String msg) {
		if (null != msg && msg.length() > 2) {
			int size = msgList.size();
			if (size > 10000) {
				int index = (int) (Math.random() * size);
				while(index < 35){
					index = (int) (Math.random() * size);
				}
				msgList.remove(index);
			}
			String temp = "";
			for (int i = 0, k = msgList.size(); i < k; i++) {
				temp = msgList.get(i);
				if (temp.equals(msg)) {
					return;
				}
			}
			msgList.add(msg);
		}
	}

	public static void main(String[] args) {
		ChatRobot cr = new ChatRobot();
		cr.createRobot(10);
		cr.startThread();
	}

}
